package com.mall.product.controller;

import com.mall.common.utils.BaseResult;
import com.mall.common.utils.PageUtils;
import com.mall.product.service.AttrAttrgroupRelationService;
import com.mall.product.service.AttrGroupService;
import com.mall.product.service.AttrService;
import com.mall.product.vo.AttrGroupRelationVo;
import com.mall.product.entity.AttrEntity;
import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.service.CategoryService;
import com.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author rose
 * @email zichuanhaoyue@gmail.com
 * @date 2021-05-16 19:10:47
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;


    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    @PostMapping("/attr/relation")
    public BaseResult addRelation(@RequestBody List<AttrGroupRelationVo>  vos) {

        attrAttrgroupRelationService.saveBatch(vos);

        return BaseResult.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/{attrgroup}/attr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public BaseResult attrRelation(@PathVariable("attrgroup") Long attrgroup){
        // PageUtils page = attrGroupService.queryPage(params);


        List<AttrEntity>  entities = attrService.getAttrRelation(attrgroup);

        return BaseResult.putData( entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/{attrgroup}/noattr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public BaseResult attrNoRelation(@RequestParam Map<String, Object> params , @PathVariable("attrgroup") Long attrgroup){
        // PageUtils page = attrGroupService.queryPage(params);


        PageUtils page = attrService.getNoAttrRelation(params,attrgroup);

        return BaseResult.putPageResult(page);
    }


    // {catelogId}/withattr
    /**
     * 列表
     */
    @GetMapping("/{catelogId}/withattr")
    //@RequiresPermissions("product:attrgroup:list")
    public BaseResult getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){

        List<AttrGroupWithAttrsVo> page = attrGroupService.getAttrGroupWithAttrsWithAttrsByCatelogId(catelogId);

        return BaseResult.putPageResult(page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public BaseResult list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        // PageUtils page = attrGroupService.queryPage(params);


        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return BaseResult.isOk(page);
    }

    /**
     *  删除
     */
    @PostMapping("/attr/relation/delete")
    //@RequiresPermissions("product:attrgroup:list")
    public BaseResult list(@RequestBody  AttrGroupRelationVo[] attrGroupRelationVo){

        attrService.deleteRelation(attrGroupRelationVo);

        return BaseResult.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public BaseResult info(@PathVariable("attrGroupId") Long attrGroupId){


		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		Long catelogId = attrGroup.getCatelogId();

		Long[] paths = categoryService.findCatelogPath(catelogId);

		attrGroup.setCatelogPath(paths);

        return BaseResult.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public BaseResult save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return BaseResult.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public BaseResult update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return BaseResult.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public BaseResult delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return BaseResult.ok();
    }

}
